package com.shijunhao.graduation.service.business.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.dto.BusinessSourcesDto;
import com.shijunhao.graduation.domain.entity.BaseEntity;
import com.shijunhao.graduation.domain.entity.BusinessSources;
import com.shijunhao.graduation.domain.entity.BusinessUserInfo;
import com.shijunhao.graduation.domain.vo.BusinessSourceInfoVo;
import com.shijunhao.graduation.domain.vo.BusinessSourcesListVo;
import com.shijunhao.graduation.domain.vo.BusinessSourcesVo;
import com.shijunhao.graduation.domain.vo.CurrentUserVo;
import com.shijunhao.graduation.exception.BusinessException;
import com.shijunhao.graduation.mapper.BusinessSourcesMapper;
import com.shijunhao.graduation.properties.RoleParams;
import com.shijunhao.graduation.service.business.IBusinessHotInfoService;
import com.shijunhao.graduation.service.business.IBusinessSourcesService;
import com.shijunhao.graduation.service.business.IBusinessUserInfoService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@Service
public class BusinessSourcesServiceImpl extends BaseServiceImpl<BusinessSourcesMapper, BusinessSources> implements IBusinessSourcesService {

    @Resource
    private IBusinessUserInfoService businessUserInfoService;
    @Resource
    private IBusinessUserInfoService userInfoService;
    @Resource
    private IBusinessHotInfoService hotInfoService;

    @Override
    public Long insert(BusinessSourcesDto dto) {
        BusinessSources businessSources = new BusinessSources();
        BeanUtils.copyProperties(dto,businessSources);
        boolean b = insertOne(businessSources);
        if(b)
            return businessSources.getFileId();
        else
            throw new BusinessException("插入资源信息失败");
    }

    @Override
    public BusinessSourcesVo toVo(BusinessSources entity) {
        BusinessSourcesVo result = new BusinessSourcesVo();
        result.setName(entity.getSourceName());
        result.setDesc(entity.getDescription());
        result.setFid(entity.getFileId());
        result.setId(entity.getId());
        result.setDate(entity.getUpdateDate());
        return result;
    }

    @Override
    public BusinessSourcesListVo toListVo(BusinessSources entity) {
        BusinessSourcesListVo result = new BusinessSourcesListVo();
        BusinessSourcesVo sourcesVo = toVo(entity);
        result.setSourceInfo(sourcesVo);
        BusinessUserInfo one = userInfoService.getOne(Wrappers.lambdaQuery(BusinessUserInfo.class).eq(BusinessUserInfo::getUid, entity.getCreateBy()));
        result.setUserInfo(one);
        return result;
    }

    @Override
    public boolean deleteById(Long id) {
        BusinessSources sources = this.getById(id);
        CurrentUserVo user = userInfoService.getCurrentOrgUser();
        if(!sources.getCreateBy().equals(user.getUser().getId()) && !RoleParams.ROLE_ADMIN.equals(user.getRole()))
            throw new BusinessException("禁止删除他人数据！！！");
        return super.deleteById(id);
    }

    @Override
    public boolean update(BusinessSources entity) {
        BusinessSources sources = this.getById(entity.getId());
        CurrentUserVo user = userInfoService.getCurrentOrgUser();
        if(!sources.getCreateBy().equals(user.getUser().getId()) && !RoleParams.ROLE_ADMIN.equals(user.getRole()))
            throw new BusinessException("禁止修改他人数据！！！");
        return super.updateById(entity);
    }

    @Override
    public BusinessSourceInfoVo toInfoVo(BusinessSources entity) {
        BusinessSourceInfoVo result = new BusinessSourceInfoVo();
        BusinessSourcesListVo listVo = toListVo(entity);
        result.setBusinessSourcesVo(listVo.getSourceInfo());
        result.setUserInfo(listVo.getUserInfo());
        //设置用户统计信息VO
        result.setUserCulVo(hotInfoService.toUserCulVo(entity.getCreateBy()));
        return result;
    }
}
